package com.fs.service;

import com.fs.entity.Customer;
import com.fs.entity.Friend;
import com.fs.entity.Room;
import com.fs.repository.CustomerRepository;
import com.fs.repository.FriendRepository;
import com.fs.repository.RoomRepository;
import com.fs.service.bo.BoPageInfo;
import com.fs.service.bo.customer.BoCustomer;
import com.fs.service.bo.friend.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
@Validated
public class FriendService {

    @PersistenceContext
    private EntityManager entityManager;

    private final FriendRepository friendRepository;
    private final CustomerRepository customerRepository;
    private final QueryUtil queryUtil;

    public FriendService(FriendRepository friendRepository, CustomerRepository customerRepository, QueryUtil queryUtil) {
        this.friendRepository = friendRepository;
        this.customerRepository = customerRepository;
        this.queryUtil = queryUtil;
    }

    public BoFriend createFriend(@NotNull @Validated BoCreateParam param) {

        Optional<Customer> optCustomer = customerRepository.findById(param.getCustomerID());
        if (!optCustomer.isPresent()) {
            throw new NotFoundException("没有找到客户");
        }

        Friend friend = new Friend();
        friend.setCustomer(optCustomer.get());
        friend.setName(param.getName());
        friend.setPhone(param.getPhone());
        friendRepository.save(friend);

        return new BoFriend(friend);
    }

    public void deleteFriend(int friendId) {
        Optional<Friend> optionalFriend = friendRepository.findById(friendId);
        if (!optionalFriend.isPresent()) {
            throw new NotFoundException("没有找到朋友");
        }

        Friend friend = optionalFriend.get();
        friend.getPurchases().clear();
        entityManager.merge(friend);

        friendRepository.deleteById(friendId);
    }

    public Integer updateFriendById(@NotNull @Validated BoUpdateParam param) {
        return friendRepository.updateFriend(param.getName(), param.getPhone(), param.getFriendId());
    }

    public BoPageInfo<BoFriendWithCustomer> query(QueryFriendParam param, int pageNo, int pageSize) {
        if (pageNo > 0 && pageSize > 0) {
            PageHelper.startPage(pageNo, pageSize, "fid desc");
        }

        List<BoFriendWithCustomer> list = queryUtil.queryFriend(param);
        if (pageNo > 0 && pageSize > 0) {
            long pages = new PageInfo<>(list).getTotal();
            return new BoPageInfo<>(list, pages);
        } else {
            return new BoPageInfo<>(list, null);
        }
    }

    public List<BoCustomer> findCustomerByFriendPhone(String friendPhone) {
        List<BoCustomer> customers = queryUtil.findCustomerByFriend(friendPhone);
        if (customers.size() == 0) {
            throw new NotFoundException("没有通过此手机号找到朋友");
        }

        return customers.stream().distinct().collect(Collectors.toList());
    }

}
